﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.Entities;
using QUANLYCHAMCONG.BL.BUS;
using DgvFilterPopup;
using ValicationCore;
using Hopthoai;

namespace QUANLYCHAMCONG.PL.CMS.Danhmuc
{
    public partial class Thamsohethong : UserControl
    {
        //set property
        int flag = 0;
        THAMSO ts;
        frmHoidap f;
        public delegate void _Close();
        public event _Close Close;
        private void SetLinks()
        {
            this.links1.Danhmuc = "Danh mục";
            this.links1.Tinhnang = "Tham Số hệ thống";
            this.links1.SetValues();
        }
        private void Event()
        {
            txttents.Focus();
            this.toolbar1.Add += new Toolbar._Add(toolbar1_Add);
            this.toolbar1.Edit += new Toolbar._Edit(toolbar1_Edit);
            this.toolbar1.Save+=new Toolbar._Save(Save);
            this.toolbar1.Delete += new Toolbar._Delete(toolbar1_Delete);
            this.toolbar1.Close += new Toolbar._Close(toolbar1_Close);
            this.toolbar1.Refresh += new Toolbar._Refesh(Refresh);
            InitGrid();
            LoadData();
        }

        void Refresh()
        {
            RemoveCheckbox();
            this.toolbar1.SetSave();
            this.flag = 0;
            this.dgvthamso.ReadOnly = true;
            this.dgvthamso.AllowUserToAddRows = true;
            LoadData();
            this.Clear();
        }
        void toolbar1_Close()
        {
            if (this.Close != null)
                this.Close();
        }
        private int FindAviable()
        {
            foreach (DataGridViewColumn dr in this.dgvthamso.Columns)
            {
                if (dr is DataGridViewCheckBoxColumn)
                    return 1;
            }
            return 0;
        }
        private void RemoveCheckbox()
        {
            if (FindAviable() == 1)
            {
                this.dgvthamso.Columns.RemoveAt(0);
                this.dgvthamso.Controls.RemoveByKey(((CheckBox)this.dgvthamso.Controls.Find("checkboxHeader", true)[0]).Name);
                LoadData();
            }
        }
        private void AddCheckbox(DataTable dt)
        {
            if (FindAviable() == 0)
            {
                DataGridViewCheckBoxColumn cb = new DataGridViewCheckBoxColumn();
                cb.Width = 30;
                cb.FillWeight = 30;
                cb.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                this.dgvthamso.Columns.Insert(0, cb);
                Rectangle rect = this.dgvthamso.GetCellDisplayRectangle(0, -1, true);
                rect.X = rect.Location.X + cb.Width/9;
                CheckBox checkboxHeader = new CheckBox();
                checkboxHeader.Name = "checkboxHeader";
                checkboxHeader.Size = new Size(18, 18);
                checkboxHeader.Location = rect.Location;
                checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
                this.dgvthamso.Controls.Add(checkboxHeader);
                this.dgvthamso.DataSource = dt;
            }
        }
        void checkboxHeader_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)this.dgvthamso.Controls.Find("checkboxHeader", true)[0]).Checked == true)
            {
                for (int i = 0; i < this.dgvthamso.RowCount; i++)
                {
                    this.dgvthamso[0, i].Value = ((CheckBox)this.dgvthamso.Controls.Find("checkboxHeader", true)[0]).Checked;
                }
            }
            else
            {
                for (int i = 0; i < this.dgvthamso.RowCount; i++)
                {
                    this.dgvthamso[0, i].Value = false;
                }
            }
        }
        void toolbar1_Delete()
        {
            flag = 3;
            this.dgvthamso.ReadOnly = false;
            this.dgvthamso.AllowUserToAddRows = false;
            this.toolbar1.SetAdd_Edit_Delete();
            AddCheckbox(THAMSOBUS.SelectAll());

        }

        void toolbar1_Edit()
        {
            flag = 2;
            Clear();
            this.dgvthamso.Focus();
            this.dgvthamso.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dgvthamso.Rows[0].Selected = true;
            this.dgvthamso.ReadOnly = false;
            this.dgvthamso.AllowUserToAddRows = false;
            this.toolbar1.SetAdd_Edit_Delete();
        }

        void toolbar1_Add()
        {
            flag = 1;
            Clear();
            CreateID();
            this.toolbar1.SetAdd_Edit_Delete();
            txttents.Focus();
        }
        THAMSO AddDuLieu(THAMSO t)
        {
            
            ts.MATS = txtmats.Text;
            ts.TENTS = txttents.Text;
            ts.THOIGIANBATDAU = dtpbd.Value;
            ts.THOIGIANKETTHUC = dtpkt.Value;
            if (cbapdung.Checked == true)
                ts.APDUNG = true;
            else
                ts.APDUNG = false;
            return ts;
        }
        void Save()
        {
            //khoi tao
            ts = new THAMSO();
            if (flag == 1)
            {
                if (THAMSOBUS.Insert(AddDuLieu(ts))==true)
                {
                    f = new frmHoidap("Thêm tham số thành công!",frmHoidap.CO.an,frmHoidap.KHONG.an,frmHoidap.THOAT.an,frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    LoadData();
                    this.toolbar1.SetSave();
                    Clear();
                }
                else
                {
                    f = new frmHoidap("Thêm tham số có lỗi xảy ra!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();  
                     LoadData();
                     this.toolbar1.SetSave();
                    Clear();
                }
            }
            if (flag == 2)
            {
                try
                {
                    f = new frmHoidap("Bạn có muốn sửa tham số ?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    if (f.ShowDialog() == DialogResult.Yes)
                    {
                        if (THAMSOBUS.Update(AddDuLieu(ts)) == true)
                        {
                            f = new frmHoidap("Sửa tham số thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                            f.ShowDialog();
                            LoadData();
                            this.dgvthamso.ReadOnly = true;
                            this.toolbar1.SetSave();
                            Clear();
                        }
                        else
                        {
                            f = new frmHoidap("Có lỗi khi Sửa tham số !", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                            f.ShowDialog();
                            this.toolbar1.SetSave();
                            this.dgvthamso.ReadOnly = true;
                            Clear();
                        }
                    }
                    else
                    {
                        this.dgvthamso.ReadOnly = true;
                        this.toolbar1.SetSave();
                        Clear();
                    }
                }
                catch { return; }
            }
            if (flag == 3)
            {
               f = new frmHoidap("Bạn có chắc chắn muốn xóa dữ liệu vừa chọn?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.dauhoi);
               if (f.ShowDialog() == DialogResult.Yes)
               {
                   foreach (DataGridViewRow dr in this.dgvthamso.Rows)
                   {
                       try
                       {
                           if (dr.Cells[0].Value!=null)
                           {
                               ts = new THAMSO();
                               ts.MATS = dr.Cells["MATS"].Value.ToString();
                               THAMSOBUS.Delete(ts);
                           }
                       }
                       catch 
                       {
                           f = new frmHoidap("Bạn chưa chọn phần tham số nào vui lòng chọn lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                           f.ShowDialog();
                       }
                   }
                   f = new frmHoidap("Xóa thành công tham số!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                   f.ShowDialog();
                   RemoveCheckbox();
                   this.toolbar1.SetSave();
                   this.dgvthamso.ReadOnly = true;
                   this.dgvthamso.AllowUserToAddRows = true;
               }
                
            }
  
        }
        void CreateID()
        {
            ts = new THAMSO();
            this.txtmats.Text = THAMSOBUS.GetID();
        }
        private void Clear()
        {
            foreach (Control c in this.Controls)
            {
                if (c is Panel)
                    foreach (Control d in c.Controls)
                        if (d is TextBox)
                            d.Text = "";
            }
        }
        void LoadData()
        {
            ts = new THAMSO();
            Common.HightLightDataGridViewRow(ref this.dgvthamso, THAMSOBUS.SelectAll());
        }
        private void InitGrid()
        {
            DgvFilterManager f = new DgvFilterManager();
            f.ColumnFilterAdding += new ColumnFilterEventHandler(f_ColumnFilterAdding);
            f.DataGridView = (DataGridView)this.dgvthamso;
        }

        void f_ColumnFilterAdding(object sender, ColumnFilterEventArgs e)
        {
            switch (e.Column.Name)
            {
                case "APDUNG":
                    {
                        e.ColumnFilter = new DgvComboBoxColumnFilter();
                        break;
                    }
                case "THOIGIANBATDAU":
                case "THOIGIANKETTHUC":
                    {
                        e.ColumnFilter = new DgvComboBoxColumnFilter();
                        break;
                    }
            }
        }
        public Thamsohethong()
        {
            InitializeComponent();
        }
        private void Thamsohethong_Load(object sender, EventArgs e)
        {
            SetLinks();
            Event();
        }
        private void dgvthamso_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (flag == 2)
            {
                try
                {
                    this.dgvthamso.EndEdit();
                    DataGridViewRow dr = this.dgvthamso.Rows[e.RowIndex];
                    txtmats.Text = dr.Cells["MATS"].Value.ToString();
                    txttents.Text = dr.Cells["TENTS"].Value.ToString();
                    if (dr.Cells["APDUNG"].Value.ToString() == "USE")
                    {
                        cbapdung.Checked = true;
                    }
                    else
                        cbapdung.Checked = false;
                    dtpbd.Value = DateTime.Parse(dr.Cells["THOIGIANBATDAU"].Value.ToString());
                    dtpkt.Value = DateTime.Parse(dr.Cells["THOIGIANKETTHUC"].Value.ToString());
                }
                catch { return; };
            }
        }

        private void dgvthamso_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
        {
            if (flag == 2)
            {
                try
                {
                    this.dgvthamso.EndEdit();
                    DataGridViewRow dr = this.dgvthamso.Rows[0];
                    txtmats.Text = dr.Cells["MATS"].Value.ToString();
                    txttents.Text = dr.Cells["TENTS"].Value.ToString();
                    if (dr.Cells["APDUNG"].Value.ToString() == "USE")
                    {
                        cbapdung.Checked = true;
                    }
                    else
                        cbapdung.Checked = false;
                    dtpbd.Value = DateTime.Parse(dr.Cells["THOIGIANBATDAU"].Value.ToString());
                    dtpkt.Value = DateTime.Parse(dr.Cells["THOIGIANKETTHUC"].Value.ToString());
                }
                catch { return; };
            }
        }

        private void dgvthamso_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (flag == 2)
            {
                try
                {
                    this.dgvthamso.EndEdit();
                    DataGridViewRow dr = this.dgvthamso.Rows[e.RowIndex];
                    txtmats.Text = dr.Cells["MATS"].Value.ToString();
                    txttents.Text = dr.Cells["TENTS"].Value.ToString();
                    if (dr.Cells["APDUNG"].Value.ToString() == "USE")
                    {
                        cbapdung.Checked = true;
                    }
                    else
                        cbapdung.Checked = false;
                    dtpbd.Value = DateTime.Parse(dr.Cells["THOIGIANBATDAU"].Value.ToString());
                    dtpkt.Value = DateTime.Parse(dr.Cells["THOIGIANKETTHUC"].Value.ToString());
                }
                catch { return; };
            }
        }
    }
}
